using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class KeyEvent : MonoBehaviour
{
    [SerializeField] Game game;
    [SerializeField] UiManager uiManager;
    [SerializeField] ClickArea[] clickAreas;

    private Controls controls;

    private void Awake()
    {
        controls = new();
    }

    private void Start()
    {
        enabled = false;
        game.GameStarted += () => enabled = true;
        game.GameEnded += () => enabled = false;
    }

    private void OnEnable()
    {
        controls.GamePlay.key1.performed += OnKey1Performed;
        controls.GamePlay.key2.performed += OnKey2Performed;
        controls.GamePlay.key3.performed += OnKey3Performed;
        controls.GamePlay.Pause.performed += OnPausePerformed;
        controls.Enable();
    }

    private void OnPausePerformed(InputAction.CallbackContext obj)
    {
        uiManager.ToggleTip();
    }

    private void OnDisable()
    {
        controls.GamePlay.key1.performed -= OnKey1Performed;
        controls.GamePlay.key2.performed -= OnKey2Performed;
        controls.GamePlay.key3.performed -= OnKey3Performed;
        controls.GamePlay.Pause.performed -= OnPausePerformed;
        controls.Disable();
    }

    private void OnKey1Performed(InputAction.CallbackContext cxt) =>
        clickAreas[0].OnPointerDown();

    private void OnKey2Performed(InputAction.CallbackContext cxt) =>
        clickAreas[1].OnPointerDown();

    private void OnKey3Performed(InputAction.CallbackContext cxt) =>
        clickAreas[2].OnPointerDown();
}
